<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\RoleService;
use Illuminate\Http\Request;

class RoleController extends BaseController
{
    //获取权限
    public function index(Request $request)
    {
        $role_id = $request->post('role_id', 0);
        $res = RoleService::index($role_id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $title = $request->post('title', '');
        if ($id) {
            $res = RoleService::modify($id, $title);
        } else {
            $res = RoleService::add($title);
        }
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = RoleService::del($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //模块列表
    public function actList(Request $request)
    {
        $role_id = $request->post('role_id', 0);
        $res = RoleService::actList($role_id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //权限设置
    public function setAuth(Request $request)
    {
        $role_id = $request->post('role_id', 0);
        $act_id = $request->post('act_id', 0);
        $res = RoleService::setAuth($role_id, $act_id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }
}